package com.mbl.base.code_generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class CodeGenerator {
    public static void main(String[] args) {
        codeGenerator("trademark", "mbl_trademark_library");
    }

    /**
     * 代码生成
     * @param modularStr 模块名称
     * @param tables 表名称，多个逗号隔开
     */
    private static void codeGenerator(String modularStr, String tables){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("lwj");
        globalConfig.setOpen(false);
        globalConfig.setSwagger2(true);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(modularStr);
        pc.setParent("com.mbl.base.modular");
        // 配置模板
//        TemplateConfig templateConfig = new TemplateConfig();
//        templateConfig.setXml(null);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.mbl.base.core.common.entity.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        strategy.setSuperControllerClass("com.mbl.base.core.common.controller.BaseController");
        strategy.setInclude(tables.split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("mbl_" + pc.getModuleName() + "_");

//        strategy.setLogicDeleteFieldName("flag"); // 设置逻辑删除字段
        strategy.setVersionFieldName("version"); // 设置乐观锁字段

        mpg.setGlobalConfig(globalConfig);
        mpg.setDataSource(getDataSource47());
        mpg.setPackageInfo(pc);
//        mpg.setTemplate(templateConfig);
        mpg.setStrategy(strategy);
//        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    /**
     * 数据源
     * @return
     */
    private static DataSourceConfig getDataSource() {
        final DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mbl_zerocode?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        return dataSourceConfig;
    }

    /**
     * 数据源
     * @return
     */
    private static DataSourceConfig getDataSource47() {
        final DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://47.104.167.116:33306/mbl?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        return dataSourceConfig;
    }

}
